const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    toggle:false,
    newName:'',
    temp:null,
    classList: [{
        name: "全部商品",
      },
      {
        name: "飲料酒水",

      },
      {
        name: "糖果餅乾",

      },
      {
        name: "粮油副食",

      },
      {
        name: "数码家电",

      },
      {
        name: "医药保健",

      },
      {
        name: "母婴玩具",

      },
      {
        name: "飲料酒水",

      },
      {
        name: "糖果餅乾",

      },
      {
        name: "粮油副食",

      },
      {
        name: "数码家电",
      },
      {
        name: "医药保健",
      },
      {
        name: "母婴玩具",
      },
    ],
    goodsList: [{
        goodsId: "0",
        goodsName: "薯片",
      goodsImage: "../../../../image/timg.jpg",
        number: 5,
        retailPrice: 10000
      },
      {
        goodsId: "1",
        goodsName: "浪味仙贝",
        goodsImage: "../../../../image/timg.jpg",
        number: 3,
        retailPrice: 25
      },
      {
        goodsId: "2",
        goodsName: "水杯水杯水杯水杯水杯水杯水杯水杯水杯水杯水杯水杯",
        goodsImage: "../../../../image/timg.jpg",
        number: 3,
        retailPrice: 2
      },
    ],
    load: true
  },

  getClass:function() {
    var that = this;
    wx.request({

      url: "http://47.106.145.141:8080/api/v1/type",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
      },
      success: function (result) {
        console.log(result)
        that.setData({
          classList:result.data.dataInfo
        })

        let list = that.data.classList;
        for (let i = 0; i < list.length; i++) {
          list[i].uid = i;
        }
        that.setData({
          list: list,
          listCur: list[0]
        })

      }
    })
  },

  getGoods:function(typenum) {
    var that = this;

    wx.request({

      url: "http://47.106.145.141:8080/api/v1/goods",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
        "pageNum": 0,
        "pageSize": 300,
        "type":typenum
  
      },
      success: function (result) {
        console.log(result.data.dataInfo.list);
        that.setData({
          goodsList: result.data.dataInfo.list
        })
        
      }
    })

    
  },



  onLoad() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    that.getClass();
    that.getGoods(0);
    
    wx.hideLoading();

    
  },
  onReady() {
    wx.hideLoading()
  },

  onShow() {
    
    var refresh = this.data.Refresh;
    if (refresh){
      wx.showLoading({
        title: '加载中...',
        mask: true
      });

      this.getClass();
      this.getGoods(0);
      wx.hideLoading();
      this.setData({
        Refresh:false,
      })
    }
  },
  addGoods: function(e) {
    wx.navigateTo({
      url: 'add-goods/add-goods'
      //点击添加跳至添加商品页
    })
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
    //console.log(this.classList[this.TabCur])
  },


  VerticalMain(e) {
    let that = this;
    let list = this.data.classList;
    
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].uid);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].uid - 1) * 50,
          TabCur: list[i].uid
        })
        return false
      }
    }
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //长按改名
  alterTab:function(e){

    this.setData({
      modalName:"Modal",
      temp:e.currentTarget.dataset,
    })
    
  },
  //提交更改的分类名
  //需要将已更改的分类名
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var that = this;
    var type = that.data.temp;
    console.log(type);
    let { newName } = e.detail.value;
    console.log(newName);
    wx.showLoading({
      title: '正在加载',
    })
    wx.request({
      url: "http://47.106.145.141:8080/api/v1/type",
      method: "PUT",
      data: {
        "token": app.globalData.key,
        "typeId": temp.typeid,
        "name": newName,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (result) {
        console.log(result)
      }
    })

    this.getClass();
    this.getGoods(0);
    wx.hideLoading();
    
   
  },

  showToggle:function(e){
    this.setData({
      toggle:true
    })
  },

  hideToggle: function (e) {
    this.setData({
      toggle: false
    })
  },

  deleteClass:function(e) {
    var that = this;
    var flag;
    var list = that.data.classList;
    console.log(e.currentTarget.dataset);
    wx.showModal({
      title: '删除',
      content: '是否删除' + e.currentTarget.dataset.name+"类别?若分类下仍有商品则会操作失败",
      showCancel:true,
      success:function(res) {
        if(res.cancel){

        }else{
          for (var i = 0; i <that.data.goodsList.length;i++){
            if (that.data.goodsList[i].typeId == e.currentTarget.dataset.id) {
              wx.showToast({
                title: '删除失败',
                icon: 'none',
                duration: 500
              })
              flag = true;
              break;
            }
          }
          console.log(i);
          if(!flag) {
            wx.request({
              url: "http://47.106.145.141/api/v1/type",
              method: "DELETE",
              data: {
                "token": app.globalData.key,
                "typeId": e.currentTarget.dataset.id
              },
              header: {
                "content-type": "application/x-www-form-urlencoded"
              },
              success: function (result) {
                console.log(result)
                list.splice(e.currentTarget.dataset.index,1);
                that.setData({
                  classList:list,
                })
                wx.showToast({
                  title: '删除成功',
                  icon: 'none',
                  duration: 500
                })
              }
            })
            
          }
        }
      }
    })
  },



  onGoodsTap:function(e){
    var goodData = JSON.stringify(e.currentTarget.dataset.gooddata);
    wx.navigateTo({
      url: 'goods-detail/goods-detail?goodData=' + goodData
      //点击添加跳至相应商品详情页（未实现相应）
    })
  },
  

  deleteGood:function(e) {
    console.log(e.currentTarget.dataset.gooddata);

    var list = this.data.goodsList;
    var that = this;

    wx.showModal({
      title: '删除',
      content: '确定删除该商品？',
      cancelText:"否",
      confirmText:"是",
      confirmColor:"red",
      success:function(res) {
        if(res.cancel) {
          wx.showToast({
            title: '取消删除',
          })
        } else {
          wx.request({
            url: "http://47.106.145.141:8080/api/v1/goods",
            method: "DELETE",
            data: {
              "token":"F8170031DB0BA03CDBC627822454E8FA",
              "goodsId": e.currentTarget.dataset.gooddata
            },
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success: function (result) {
              console.log(result.data.code);
              if (result.data.code==1) {
                for (let i = 0; i < list.length; i++) {
                  if (list[i].id == e.currentTarget.dataset.gooddata) {
                    list.splice(i, 1);
                    wx.showToast({
                      title: '已删除',
                    })
                    that.setData({
                      goodsList: list,
                    })
                  }

              }             
              }
            }
          })                   
        }
      }
    });

    
    
  },
  
})